package ru.agiledev.akb.client.window;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import ru.agiledev.akb.client.RegistrationService;
import ru.agiledev.akb.client.RegistrationServiceAsync;

/**
 * Created by a.kirillov
 * Date: 4/2/11 Time: 2:29 PM
 */
public class SendPasswordWindow extends DialogBox {
    private final RegistrationServiceAsync regService = GWT.create(RegistrationService.class);
    private TextBox user;
    HorizontalPanel errorPanel;
    private Button confirm;
    private Button cancel;

    public SendPasswordWindow(int x, int y){
        setModal(true);
        setText("Восстановление пароля");
        setAnimationEnabled(true);
        addStyleName("forgotPasswordDialogBox");

        setPopupPosition(x, y);

        VerticalPanel mainPanel = new VerticalPanel();
        mainPanel.setSpacing(4);
        mainPanel.add(new HTML("Введите имя пользователя или e-mail. На указанный при регистрации e-mail будет выслан пароль."));
        user = new TextBox();
        user.setWidth("312px");

        user.addFocusHandler(new FocusHandler() {
            @Override
            public void onFocus(FocusEvent event) {
                if(errorPanel.isVisible()){
                    errorPanel.setVisible(false);
                }
            }
        });

        mainPanel.add(user);

        errorPanel = new HorizontalPanel();
        errorPanel.setStyleName("loginErrorPanel");
        errorPanel.setVisible(false);
        errorPanel.setSpacing(4);

        mainPanel.add(errorPanel);

        HorizontalPanel buttons = new HorizontalPanel();
        buttons.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE);
        buttons.setSpacing(4);
        confirm = new Button("Отправить");
        cancel = new Button("Отмена");

        confirm.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                resetPassword();
            }
        });

        cancel.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent clickEvent) {
                hide();
            }
        });

        buttons.add(confirm);
        buttons.add(cancel);

        mainPanel.add(buttons);

        add(mainPanel);
    }

    protected void showError(String text){
        HTML errorText = new HTML(text);
        if((errorPanel.getWidgetCount() != 0)&&(errorPanel.getWidget(0)!=null)){
            errorPanel.remove(0);
        }
        errorPanel.add(errorText);
        errorPanel.setVisible(true);
    }

    private void resetPassword(){
            AsyncCallback callback = new AsyncCallback() {
                public void onSuccess(Object result) {
                    enable(true);
                     hide();
                    final DialogBox dialogBox = new DialogBox();
                        dialogBox.setPopupPosition(RootPanel.get().getOffsetWidth()/2-160, RootPanel.get().getOffsetHeight()/2-150);
                        dialogBox.setText("Пароль изменен");
                        dialogBox.setWidth("310px");
                        dialogBox.setHeight("100px");
                        dialogBox.setModal(true);

                        VerticalPanel dialogPanel = new VerticalPanel();
                        dialogPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);


                        dialogPanel.add(new HTML("<center>Пароль был обновлен. Проверьте почту.</center>"));
                        HorizontalPanel dialogBoxButtons = new HorizontalPanel();
                        dialogBoxButtons.setSpacing(4);
                        dialogBoxButtons.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
                        Button ok = new Button("Ок");
                        ok.addClickHandler(new ClickHandler() {
                            @Override
                            public void onClick(ClickEvent clickEvent) {
                                dialogBox.hide();
                            }
                        });
                        dialogBoxButtons.add(ok);
                        dialogPanel.add(dialogBoxButtons);
                        dialogBox.add(dialogPanel);
                        dialogBox.show();
                }

                public void onFailure(Throwable ex) {
                    enable(true);
                      showError(ex.getMessage());

                }
            };
            //showLoading(true);
            enable(false);
            regService.resetPassword(user.getText(), callback);
    }

    void enable(boolean flag){
        user.setEnabled(flag);
        confirm.setEnabled(flag);
        cancel.setEnabled(flag);
    }
}